package com.uit.se6.customui.android.navigation.SideDrawer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uit.se6.customui.R;

class SideDrawerListAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<SideDrawerItem> listDrawerItems;

	public SideDrawerListAdapter(Context context,
			ArrayList<SideDrawerItem> navDrawerItems) {
		this.context = context;
		this.listDrawerItems = navDrawerItems;
	}

	@Override
	public int getCount() {
		return listDrawerItems.size();
	}

	@Override
	public Object getItem(int position) {
		return listDrawerItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.sidedrawer_list_item, null);

			viewHolder = new ViewHolder();
			viewHolder.imgIcon = (ImageView) convertView
					.findViewById(R.id.icon);

			viewHolder.txtTitle = (TextView) convertView
					.findViewById(R.id.title);
			viewHolder.txtCount = (TextView) convertView
					.findViewById(R.id.counter);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.imgIcon.setImageResource(listDrawerItems.get(position)
				.getIcon());
		viewHolder.txtTitle.setText(listDrawerItems.get(position).getTitle());

		// displaying count
		// check whether it set visible or not
		if (listDrawerItems.get(position).getCounterVisibility()) {
			viewHolder.txtCount.setText(listDrawerItems.get(position)
					.getCount());
		} else {
			// hide the counter view
			viewHolder.txtCount.setVisibility(View.GONE);
		}

		return convertView;
	}

	static class ViewHolder {
		ImageView imgIcon;
		TextView txtTitle;
		TextView txtCount;
	}
}
